package applet.attendee.unlocked.components.checkIn;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import applet.attendee.CMSAttendeeView;
import applet.attendee.locked.CMSAttendeeLockedView;
import applet.misc.graphics.AttendeeBackgroundPanel;
import applet.misc.web.WebsiteActionListener;

import data.CMSRes;
import data.account.attendee.AttendeeAccount;
import data.account.attendee.lodging.LodgingData;
import data.account.attendee.travel.TravelData;

public class CMSCheckInDialog extends JDialog
{
	private static final long serialVersionUID = -4599213533605568355L;

	private JLabel conferenceCost;
	private JLabel lodgingCost;
	private JLabel travelCost;
	private JLabel totalCost;

	private JComboBox creditCardProvider;
	private JTextField creditCardNumber;
	private JTextField creditCardSecCode;
	
	private CMSAttendeeView attendeeView;
	
	public CMSCheckInDialog(CMSAttendeeView attendeeView)
	{
		super(CMSRes.frame(), "Check In", true);
		this.attendeeView = attendeeView;
		setSize(400, 500);
		setMinimumSize(new Dimension(500, 500));
		
		defineComponents(attendeeView.attendee());
		
		setLayout(new BorderLayout());
			JPanel layoutHelper = new AttendeeBackgroundPanel(new BorderLayout());
			layoutHelper.add(getBottomBar(), BorderLayout.SOUTH);
			layoutHelper.add(getPaymentPanel(), BorderLayout.CENTER);
		add(layoutHelper, BorderLayout.CENTER);
		
		pack();
		validate();
	}
	
	private void defineComponents(AttendeeAccount attendee)
	{
		int cost = 50;
		conferenceCost = new JLabel("$" + cost + ".00");
		conferenceCost.setFont(conferenceCost.getFont().deriveFont(Font.BOLD));
		
		LodgingData lodging = attendee.lodging();
		if(lodging != null)
			cost += lodging.cost();
		lodgingCost = new JLabel(lodging == null ? "(none)" : "$" + lodging.cost() + ".00");
		lodgingCost.setFont(lodgingCost.getFont().deriveFont(Font.BOLD));
		
		TravelData travel = attendee.travel();
		if(travel != null)
			cost += travel.cost();
		travelCost = new JLabel(travel == null ? "(none)" : "$" + travel.cost() + ".00");
		travelCost.setFont(lodgingCost.getFont().deriveFont(Font.BOLD));
		
		totalCost = new JLabel("$" + cost + ".00");
		totalCost.setFont(conferenceCost.getFont().deriveFont(Font.BOLD));
		
		creditCardProvider = new JComboBox(new String[] {"Visa", "Mastervard", "CitiBank" });
		creditCardProvider.setPreferredSize(new Dimension(150, 30));
		
		creditCardNumber = new JTextField(15);
		creditCardNumber.setPreferredSize(new Dimension(150, 30));
		
		creditCardSecCode = new JTextField(4);
		creditCardSecCode.setPreferredSize(new Dimension(50, 30));
	}
	
	private JPanel getPaymentPanel()
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setOpaque(false);
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
			layoutHelper.setOpaque(false);
				JLabel labelHelper = new JLabel("Check-In for " + attendeeView.attendee().fullName());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				labelHelper.setFont(labelHelper.getFont().deriveFont(16f));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 3));
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Conference cost:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(conferenceCost, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 3));
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Travel cost:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(travelCost, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 3));
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Lodging cost:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(lodgingCost, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 3));
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Total cost:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(totalCost, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Credit Card Provider:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(creditCardProvider, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Number:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(creditCardNumber, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
				labelHelper = new JLabel("Security Code:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.setOpaque(false);
			layoutHelper.add(creditCardSecCode, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JButton cancelPayment = new JButton("Cancel");
				cancelPayment.setPreferredSize(new Dimension(200, 30));
				cancelPayment.addActionListener(new CancelCheckInListener());
			layoutHelper.add(cancelPayment, BorderLayout.CENTER);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JButton completePayment = new JButton("Finish Payment");
				completePayment.setPreferredSize(new Dimension(200, 30));
				completePayment.addActionListener(new CompleteCheckInListener());
			layoutHelper.add(completePayment, BorderLayout.CENTER);
		panel.add(layoutHelper);
		
		return panel;
	}
	
	private JPanel getBottomBar()
	{
		JPanel panel = new JPanel(new GridBagLayout());
		panel.setOpaque(false);
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
			JButton thirdPartyButton = new JButton("Pay with Paypal");
			thirdPartyButton.setPreferredSize(new Dimension(200, 30));
			thirdPartyButton.addActionListener(new WebsiteActionListener("http://www.paypal.com"));
			thirdPartyButton.addActionListener(new CancelCheckInListener());
		panel.add(thirdPartyButton);
		return panel;
	}
	
	private class CancelCheckInListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			dispose(); } }
	
	private class CompleteCheckInListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			
			boolean canCheckIn = false;
			
			try{
				if(	Integer.parseInt(creditCardNumber.getText()) != 0 &&
					Integer.parseInt(creditCardSecCode.getText()) != 0)
					canCheckIn = true;
			}
			catch(Exception exception) { canCheckIn = false; }
			
			if(canCheckIn)
			{
				attendeeView.attendee().checkIn();
				attendeeView.attendeeViewController().deferToView(new CMSAttendeeLockedView(attendeeView));
				dispose();
			}
			else
			{
				JOptionPane.showMessageDialog(CMSRes.frame(),
					    "Payment cannot be completed - Credit card data has not been filled out!!",
					    "Payment Failed",
					    JOptionPane.WARNING_MESSAGE);
			} } }
}